<template class="cate-container">
  <div class="cate-container">
    <el-card>
      <div class="cate-heade">
        <Bread first="文章管理" second="文章分类"></Bread>
        <el-button @click="addCate">添加</el-button>
      </div>

      <el-table :data="artList" border style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称" width="180">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      width="40%"
      title="添加文章分类"
      :visible.sync="dialogFormVisible"
      @close="close"
    >
      <el-form ref="addCateForm" :model="form" :rules="addRules">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input v-model="form.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input v-model="form.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addHandle">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog
      width="40%"
      title="添加文章分类"
      :visible.sync="dialogFormVisibleEdit"
      @close="close2"
    >
      <el-form ref="EditCateForm" :model="formEdit" :rules="addRules">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input v-model="formEdit.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input v-model="formEdit.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="emitHandle">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addCateApi, deleteApi, delCateByidApi } from '@/api/art'

import { mapState, mapActions } from 'vuex'
export default {
  data () {
    return {
      dialogFormVisible: false,
      dialogFormVisibleEdit: false,
      form: {
        cate_name: '',
        cate_alias: ''
      },
      formEdit: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions('articel', ['AgetArtList']),
    handleEdit (row) {
      this.dialogFormVisibleEdit = true
      this.formEdit = row
    },
    // 删除文章
    handleDelete (row) {
      this.$confirm('此操作将永久删除,是否继续', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deleteApi(row.id)
        // console.log(res)
        if (res.data.code === 0) {
          this.$message({
            message: '恭喜删除成功',
            type: 'success'
          })
          // 添加成功后重新渲染页面
          this.AgetArtList()
        }
      })
    },
    // 对话框打开
    addCate () {
      this.dialogFormVisible = true
    },
    // 添加
    close () {
      this.$refs.addCateForm.resetFields()
    },
    // 编辑
    close2 () {
      this.$refs.EditCateForm.resetFields()
    },
    // 添加
    addHandle () {
      this.$refs.addCateForm.validate(async (valid) => {
        if (valid) {
          const res = await addCateApi(this.form)
          if (res.data.code === 0) {
            this.$message({
              message: '恭喜添加成功',
              type: 'success'
            })
            // 添加成功后重新渲染页面
            this.AgetArtList()
            // 关闭对话框
            this.dialogFormVisible = false
          }
        }
      })
    },
    // 编辑
    emitHandle () {
      this.$refs.EditCateForm.validate(async (valid) => {
        if (valid) {
          const res = await delCateByidApi(this.formEdit)
          if (res.data.code === 0) {
            this.$message({
              message: '恭喜更新成功',
              type: 'success'
            })
            // 更新成功后重新渲染页面
            this.AgetArtList()
            // 关闭对话框
            this.dialogFormVisibleEdit = false
          }
        }
      })
    }
  },
  created () {
    this.AgetArtList()
  },
  computed: {
    ...mapState('articel', ['artList'])
  }
}
</script>

<style  lang='less'  scoped>
.cate-container {
  .cate-heade {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
